//
//  StarView.m
//  卢卡的时光网
//
//  Created by 卡神 on 15/7/21.
//  Copyright © 2015年 lok. All rights reserved.
//

#import "StarView.h"
#import "UIViewExt.h"

@implementation StarView

- (nonnull instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self _createView];
    }
    return self;
}

- (void)awakeFromNib {
    //[self setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"bg_main"]]];
    [self _createView];
}

- (void)_createView {
    UIImage *yellow = [UIImage imageNamed:@"yellow"];
    UIImage *gray = [UIImage imageNamed:@"gray"];
    
    
    _yellowView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, yellow.size.width * 5, yellow.size.height)];
    _grayView = [[UIView alloc] initWithFrame: CGRectMake(0, 0, gray.size.width * 5, gray.size.height)];
    
    [_yellowView setBackgroundColor:[UIColor colorWithPatternImage:yellow]];
    [_grayView setBackgroundColor:[UIColor colorWithPatternImage:gray]];
    //设置锚点有些偏差
    //        _yellowView.layer.anchorPoint = CGPointMake(0, 0);
    //        _grayView.layer.anchorPoint = CGPointMake(0, 0);
    
    CGFloat scale1 = self.bounds.size.height / _grayView.frame.size.height;
    _grayView.transform = CGAffineTransformMakeScale(scale1, scale1);
    _yellowView.transform = CGAffineTransformMakeScale(scale1, scale1);
    CGRect rect = _yellowView.frame;
    rect.origin = CGPointMake(0, 0);
    CGRect grayFrame = _grayView.frame;
    grayFrame.origin = CGPointMake(0, 0);
    
    _grayView.frame = grayFrame;
    _yellowView.frame = rect;
    
    [self addSubview:_grayView];
    [self addSubview:_yellowView];
    
}

- (void)setAverage:(double)average {
    _average = average;
    [self setNeedsLayout];
}

- (void)layoutSubviews {
    _yellowView.width = _grayView.width *  _average / 10;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
